package com.bobo.service.impl;
import com.bobo.entity.Vegetable;
import com.bobo.mapper.VegetableMapper;
import com.bobo.vo.VegetableVO;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.bobo.constant.MessageConstant;
import com.bobo.constant.StatusConstant;
import com.bobo.dto.VegetableDTO;
import com.bobo.dto.VegetablePageQueryDTO;
import com.bobo.entity.DishFlavor;
import com.bobo.entity.Setmeal;
import com.bobo.exception.DeletionNotAllowedException;
import com.bobo.mapper.DishFlavorMapper;
import com.bobo.mapper.SetmealDishMapper;
import com.bobo.mapper.SetmealMapper;
import com.bobo.result.PageResult;
import com.bobo.service.VegetableService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
@Slf4j
public class VegetableServiceImpl implements VegetableService {
    @Autowired
    private VegetableMapper dishMapper;
    @Autowired
    private DishFlavorMapper dishFlavorMapper;
    @Autowired
    private SetmealDishMapper setmealDishMapper;
    @Autowired
    private SetmealMapper setmealMapper;
    /**
     * 新增菜品和对应的口味数据
     * @param vegetableDTO
     */
    @Transactional
    public void savewithFlavor(VegetableDTO vegetableDTO){
        Vegetable vegetable = new Vegetable();
        BeanUtils.copyProperties(vegetableDTO, vegetable);

        //向菜品表插入数据1条
        dishMapper.insert(vegetable);
        //获取insert语句生成的主键值
        Long dishId = vegetable.getId();

        //向口味表插入n条数据
        List<DishFlavor> flavors = vegetableDTO.getFlavors();
        if(flavors != null&&flavors.size()>0){
            flavors.forEach(dishFlavor ->{
                dishFlavor.setDishId(dishId);
            });
            //插入n条数据
            dishFlavorMapper.insertBatch(flavors);
        }


    }

    /**
     * 菜品分页查询
     * @param vegetablePageQueryDTO
     * @return
     */
    public PageResult pageQuery(VegetablePageQueryDTO vegetablePageQueryDTO){
        PageHelper.startPage(vegetablePageQueryDTO.getPage(), vegetablePageQueryDTO.getPageSize());
        Page<VegetableVO> page = dishMapper.pageQuery(vegetablePageQueryDTO);
        return new PageResult(page.getTotal(),page.getResult());
    }

    /**
     * 菜品的批量删除
     * @param ids
     * @return
     */
    public void deleteBatch(List<Long> ids) {
        //判断当前菜品是否能够删除---是否存在起售中
        for (Long id : ids) {
            Vegetable vegetable = dishMapper.getById(id);
            if(vegetable.getStatus()== StatusConstant.ENABLE){
                //当前菜品起售中不能删除
                throw new DeletionNotAllowedException(MessageConstant.DISH_ON_SALE);
            }
        }

        //判断........--是否被套餐关联
        List<Long> setmealIds = setmealDishMapper.getSetmealIdsByDishIds(ids);
        if(setmealIds != null && setmealIds.size()> 0){
            //当前菜品被套餐关联不能删除
            throw new DeletionNotAllowedException(MessageConstant.DISH_BE_RELATED_BY_SETMEAL);


        }
        //删除菜品表中的菜品数据
        /*for (Long id : ids) {
            dishMapper.deleteById(id);
            //删除口味数据
            dishFlavorMapper.deleteByDishId(id);

        }*/
        //根据菜品id集合批量删除菜品数据
        //delete from dish where id in (?,?,?)
        dishMapper.deleteByIds(ids);

        //批量删除关联的口味数据
        //delete from dish_flavor where dish_id in (?,?,?)
        dishFlavorMapper.deleteByDishIds(ids);

    //delete from dish where id in (?,?,?)

    }
    /**
     * 根据id查询菜品和对应的口味数据
     * @param id
     * @return
     */
    public VegetableVO getByIdWithFlavor(Long id) {
        //根据id查询菜品数据
        Vegetable vegetable = dishMapper.getById(id);
        //根据菜品id查询口味数据
        List<DishFlavor> dishFlavors = dishFlavorMapper.getByDishId(id);
        //查询到的数据封装到DishaVO
        VegetableVO vegetableVO = new VegetableVO();
        BeanUtils.copyProperties(vegetable, vegetableVO);
        vegetableVO.setFlavors(dishFlavors);
        return vegetableVO;
    }
    /**
     * 根据id修改菜品基本信息和对应的口味信息
     * @param vegetableDTO
     */
    @Transactional
    public void updateWithFlavor(VegetableDTO vegetableDTO) {
        Vegetable vegetable = new Vegetable();
        BeanUtils.copyProperties(vegetableDTO, vegetable);
        //修改菜品表基本信息
        dishMapper.update(vegetable);
        //修改口味表（删除原有的口味数据再重新插入口味数据）
        //删除
        dishFlavorMapper.deleteByDishId(vegetableDTO.getId());
        //插入
        List<DishFlavor> flavors = vegetableDTO.getFlavors();
        if(flavors != null&& flavors.size()>0){
            flavors.forEach(dishFlavor ->{
                dishFlavor.setDishId(vegetableDTO.getId());
            });
            dishFlavorMapper.insertBatch(flavors);
        }

    }

    /**
     * 根据套餐id查询菜品
     * @param categoryId
     * @return
     */
    public List<Vegetable> list(Long categoryId) {
        Vegetable vegetable = Vegetable.builder()
                .categoryId(categoryId)
                .status(StatusConstant.ENABLE)
                .build();
        return dishMapper.list(vegetable);
    }

    /**
     * 菜品起售停售
     * @param status
     * @param id
     */
    @Transactional
    public void startOrStop(Integer status, Long id) {
        Vegetable vegetable = Vegetable.builder()
                .id(id)
                .status(status)
                .build();
        dishMapper.update(vegetable);
        if(status==StatusConstant.DISABLE){
            //如果是停售操作，还要将包含当前菜品的套餐也停售
            List<Long> dishIds = new ArrayList<>();
            dishIds.add(id);
            //select setmeal id from setmeal dish where dish id in(?,?,?)
            List<Long> setmealIds = setmealDishMapper.getSetmealIdsByDishIds(dishIds);
            if(setmealIds !=null && setmealIds.size()>0){
                for(Long setmealId : setmealIds){
                    Setmeal setmeal = Setmeal.builder()
                            .id(id)
                            .status(StatusConstant.DISABLE)
                            .build();
                    setmealMapper.update(setmeal);
                }
            }
        }
    }
    /**
     * 条件查询菜品和口味
     * @param vegetable
     * @return
     */
    public List<VegetableVO> listWithFlavor(Vegetable vegetable) {
        List<Vegetable> vegetableList = dishMapper.list(vegetable);

        List<VegetableVO> vegetableVOList = new ArrayList<>();

        for (Vegetable d : vegetableList) {
            VegetableVO vegetableVO = new VegetableVO();
            BeanUtils.copyProperties(d, vegetableVO);

            //根据菜品id查询对应的口味
            List<DishFlavor> flavors = dishFlavorMapper.getByDishId(d.getId());

            vegetableVO.setFlavors(flavors);
            vegetableVOList.add(vegetableVO);
        }

        return vegetableVOList;
    }
}
